package com.cci.kangdao.utilTool;


import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.concurrent.*;

/**
 * @description:
 * @author: shiliqiang
 * @company: CTY Technology Co.,Ltd
 * @since: 2021/11/18 11:11
 * @version: V1.0
 */
public class ThreadPoolUtil {

    /**
     * 自定义线程名称,方便的出错的时候溯源
     */
    private static ThreadFactory threadFactoryName = new CustomizableThreadFactory("synCrmThread");


    private static ExecutorService threadPoolService = new ThreadPoolExecutor(
            5,
            10,
            0L,
            TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(4),
            threadFactoryName,
            new ThreadPoolExecutor.AbortPolicy()
    );

    /**
     * 获取线程池
     * @return 线程池
     */
    public static ExecutorService getEs() {
        return threadPoolService;
    }

    /**
     * 使用线程池创建线程并异步执行任务
     * @param r 任务
     */
    public static void newTask(Runnable r) {
        threadPoolService.execute(r);
    }

    public static void shutdown() {
        threadPoolService.shutdown();
    }
    public static void main(String[] args) {
//        for (int i = 0;i<20;i++){
//            int finalI = i;
//            Runnable r = new Runnable() {
//                @Override
//                public void run() {
//                    System.out.println("当前线程为:"+ finalI);
//                    try {
//                        Thread.sleep(1*1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            };
//            newTask(r);
//        }
    }

}
